
function gotoPageInPromo(pageNo) {
		var frm=$('couponListForm');
		frm.action = frm.action + (frm.action.match(/\?/) ? '&' : '?') + "PrmPageNo="+pageNo;
		var prmItemsPerPage= document.getElementById("PrmItemsPerPage").value;
		$j('#ruleDetail-couponList').load( frm.action+"&PrmItemsPerPage="+prmItemsPerPage+"&promoRule="+$j('#couponListForm #promoRule').val()+"&decorator=blank");
}


function fnDoMultiDeleteInPromo(obj) {
	var itemNames = fnGetSelectedItemNames();
	var frm=document.couponListForm;
	
	//get the delete multiIds
	if (itemNames=="") {
		return false;
	}
	var multiIds = document.getElementsByName("multiIds");
	var multiIdString = "";
	for(var i=0; i< multiIds.length; i++){
		if(multiIds[i].checked){
			multiIdString += "&multiIds="+multiIds[i].value;
		}
	}	
	if (confirm(__FMT.common_message_confirmDeleteThis+ " " + itemNames+"?")) {
		//delete first by ajax
		$j.post(frm.action+"?doAction=multiDelete"+multiIdString+"&decorator=blank","",multiDeleteCallback);
		//update message
   		//$j('#coupon_message').empty();
   		//$j('#coupon_message').append(pmsg.coupon_commit_wait);
	}	
}

function multiDeleteCallback(){
	//fill the div
	var frm=document.couponListForm;
	var postData = $j(frm).find(':input').serializeArray();
	$j('#ruleDetail-couponList').load(frm.action+"?doAction=search"+"&decorator=blank",postData);
	//update message
   	//$j('#coupon_message').empty();
   	//$j('#coupon_message').append(pmsg.coupon_commit_success);
}

function validateGenCouponForm(){
	applyValidate($("quantity"),"required,integer,minValue=1,maxValue=500");
	applyValidate($("prefix"),"code");
	if (validateForm($('genCouponForm'))) {
  		
  		return true;
  	} 
  	else {
  		alert(__vaMsg.notPass); 
  		return false; 
  	}
	
	
}

function genCoupons(){
   	$j('#coupon_message').empty();
	if($("promoRuleId").value == ''){
		alert(pmsg.coupon_gen_after_save);
		return false;
	}
	if(!validateGenCouponForm()){
		return false;
	}
	//get the url  	
    var url=$j('#promoRuleForm').attr('action')+"?doAction=generateCoupon";
	var postData="";
    postData += $j('#promoRule :input').serialize();
  	
    //alert(postData);
    $j.post(url,postData,genCouponCallback);
    //update message
   	$j('#coupon_message').append('<p class="cont">'+pmsg.coupon_commit_wait+'</p>').show();
}

function genCouponCallback(){
   		$j('#coupon_message').empty();
		//update message
		$j('#ruleDetail-couponList').load(__ctxPath+"/sales/coupon.html?promoRule="+$j('#promoRuleId').val()+"&doAction=search"+"&decorator=blank");
			
   		$j('#coupon_message').append('<p class="cont">'+pmsg.coupon_commit_success+'</p>').show();
   		
}

function validateSendCouponForm(){
	applyValidate($("firstName"),"maxlength=10");
	applyValidate($("email"),"required,email,maxlength=50");
	applyValidate($("couponNo"),"required,maxlength=30");
	if (bCancel) return true; 
  	else if (validateForm($('sendCouponForm'))) {
  		removeValidate($("firstName"));
  		removeValidate($("email"));
  		removeValidate($("couponNo"));
  		return true;
  	} 
  	else {
  		alert(__vaMsg.notPass); 
  		return false; 
  	}
	
	
}

function fnInitSendCouponForm(couponNo){
	$j('#couponNo').val(couponNo);
	
}

function sendCoupon(){
   	$j('#coupon_message').empty();
	if(!validateSendCouponForm()){
		return false;
	}
	//get the url  	
    var url=$j('#promoRuleForm').attr('action')+"?doAction=sendCoupon";
    var postData = $j('#sendCouponForm :input').serialize();
    //alert(postData);
    $j.post(url,postData,sendCouponCallback);
    //close dialog
    dlgsendCouponDialog_close();
    //update message
   	$j('#coupon_message').append('<p class="cont">'+pmsg.coupon_commit_wait+'</p>').show();
   	
}

function sendCouponCallback(){
   		$j('#coupon_message').empty();
		//update message
		$j('#ruleDetail-couponList').load(__ctxPath+"/sales/coupon.html?promoRule="+$j('#promoRuleId').val()+"&doAction=search"+"&decorator=blank");	
   		$j('#coupon_message').append('<p class="cont">'+pmsg.send_coupon_success+'</p>').show();
   		
}
